<?php

namespace app\admin\controller\polite;

use app\admin\model\PoliteConfig;
use app\admin\model\PoliteConfigType;
use EasyAdmin\annotation\NodeAnotation;
use app\admin\traits\Curd;
use app\common\controller\AdminController;
use think\App;
use EasyAdmin\annotation\ControllerAnnotation;

/**
 * Class Goods
 * @package app\admin\controller\polite
 * @ControllerAnnotation(title="智慧乡村参数配置管理")
 */
class Config extends AdminController
{

    use Curd;

    protected $relationSearch = false;
    protected $configTypeM;

    public function __construct(App $app)
    {
        parent::__construct($app);
        $this->model = new PoliteConfig();
        $this->configTypeM = new PoliteConfigType();
    }

    /**
     * @NodeAnotation(title="参数配置列表")
     */
    public function index()
    {
        if ($this->request->isAjax()) {
            if (input('selectFields')) {
                return $this->selectList();
            }
            list($page, $limit, $where) = $this->buildTableParames();
            $count = $this->model->where($where)->count();
            $list = $this->model->where($where)->page($page, $limit)->order($this->sort)->select();
            foreach ($list as $k=>$v){
                $list[$k]=$this->resetInfo($v);
            }
            $data = [
                'code'  => 0,
                'msg'   => '',
                'count' => $count,
                'data'  => $list,
            ];
            return json($data);
        }
        return $this->fetch();
    }

    /**
     * @NodeAnotation(title="添加参数配置")
     */
    public function add($id = null)
    {
        $configType=$this->configTypeM->select()->toArray();
        if ($this->request->isAjax()) {
            $post = $this->request->post();
            $rule = [
                'config_type|配置项'   => 'require',
                'config_value|参数值' => 'require',
                'config_order|排序'  => 'require',
            ];
            $this->validate($post, $rule);
            try {
                $save = $this->model->save($post);
            } catch (\Exception $e) {
                $this->error('保存失败');
            }
            if ($save) {
                $this->success('保存成功');
            } else {
                $this->error('保存失败');
            }
        }
        $this->assign([
            'configType'=>$configType,
        ]);
        return $this->fetch();
    }

    /**
     * @NodeAnotation(title="编辑参数配置")
     */
    public function edit($id)
    {
        $row = $this->model->find($id);
        empty($row) && $this->error('数据不存在');
        $configType=$this->configTypeM->select()->toArray();
        if ($this->request->isAjax()) {
            $post = $this->request->post();
            $rule = [
                'config_type|配置项'   => 'require',
                'config_value|参数值' => 'require',
                'config_order|排序'  => 'require',
            ];
            $this->validate($post, $rule);
            try {
                $save = $row->save($post);
            } catch (\Exception $e) {
                $this->error('保存失败');
            }
            if ($save) {
                $this->success('保存成功');
            } else {
                $this->error('保存失败');
            }
        }
        $this->assign([
            'id'          => $id,
            'row'         => $row,
            'configType'=>$configType,
        ]);
        return $this->fetch();
    }
    /**
     * @NodeAnotation(title="删除参数配置")
     */
    public function delete($id)
    {
        $row = $this->model->whereIn('id', $id)->select();
        $row->isEmpty() && $this->error('数据不存在');
        try {
            $save = $row->delete();
        } catch (\Exception $e) {
            $this->error('删除失败');
        }
        $save ? $this->success('删除成功') : $this->error('删除失败');
    }

    private function resetInfo($data){
        if($data){
            if($data['config_type']){
                $data['config_type_text']=$this->configTypeM->where(['id'=>$data['config_type']])->find()->toArray()['type_name'];
            }
        }
        return $data;
    }

}